package com.wq.wrongquestion.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.wq.wrongquestion.entity.chi.ChineseEntity;
import com.wq.wrongquestion.service.chi.ChineseService;
import com.wq.wrongquestion.utils.FileUploadUtils;

@Controller
@RequestMapping("/chi")
public class ChineseController {
    @Autowired
    public ChineseService chineseService;
    @Autowired
    public FileUploadUtils fileUploadUtils;

    final String IMG_PAHT_YT_CHI = "images/chi/yt/";
    final String IMG_PAHT_DA_CHI = "images/chi/da/"; 

   /************************************************Chinese Start************************************************/
    @RequestMapping("/chineseInit") 
    public String chineseInit(Model model){
        //章节
        List<String> allZj=chineseService.getChineseZj();
        
        model.addAttribute("allZj", allZj);
        model.addAttribute("condEntity", new ChineseEntity());
        
        return "chi/chineseSearch";
    }


    @PostMapping("/chineseIns")
    public String insChinese(ChineseEntity chineseEntity, @RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile, Model model) {
        // 获取文件名
        String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
        String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();

        // 获取文件路径
        String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_CHI);
        String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_CHI);

        // 保存文件
        try {
            if (ytFile.getSize() > 0) {
                fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                chineseEntity.setImgytnm(fileName);
                chineseEntity.setImgytpath(filePath);
            }
            if (daFile.getSize() > 0) {
                fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                chineseEntity.setImgdanm(fileName2);
                chineseEntity.setImgdapath(filePath2);
            }
            // 插入数据
            chineseEntity.setFxcs(1);
            chineseEntity.setXueke(chineseEntity.getHidXueke());

            chineseService.insChinese(chineseEntity);

            // return "redirect:/chi/chineseInit";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "发生错误,请重试!");
            return "redirect:/chi/addChinese";
        }
        return "redirect:/chi/chineseInit";
    }
    @RequestMapping("/addChinese")
    public String addChinese(Model model){
        List<String> allZj=chineseService.getChineseZj();
        
        model.addAttribute("allZj", allZj);
        return "chi/addChinese";
    }

    @RequestMapping("/searchChinese")
    public String searchChinese(Model model, @RequestParam(value = "pageNm",defaultValue= "1") Integer pageNum,
            @RequestParam(value="pageSize" ,defaultValue= "10") Integer pageSize, ChineseEntity chineseEntity) {

        PageInfo<ChineseEntity> chinese = chineseService.getChinese(pageNum, pageSize, chineseEntity);
        List<String> allZj=chineseService.getChineseZj();

        model.addAttribute("allZj", allZj);
        model.addAttribute("chinese", chinese);
        model.addAttribute("condEntity", chineseEntity);

        return "chi/chineseSearch";
    }

    @RequestMapping("/searchChineseByID")
    public String searchChineseByID(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {

        List<ChineseEntity> chineseByID = chineseService.getChineseByID(id);
        List<String> allZj=chineseService.getChineseZj();
        if(!chineseByID.isEmpty()){
            model.addAttribute("chinese", chineseByID);
        }else{
            model.addAttribute("msg", "这条记录在数据库中不存在！");
            return "chi/chineseSearch";
        }
        
        model.addAttribute("allZj", allZj);

        return "chi/modifyChinese";
    }

    @PostMapping("/chineseUpd")
    public String chineseUpd(Model model, ChineseEntity chineseEntity,@RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile) {
        try {
            // 图片比较
            if(ytFile != null && ytFile.getOriginalFilename() !=null){
                if(!ytFile.getOriginalFilename().startsWith("yt", 0)){
                    // 获取文件名
                    String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_CHI);
                    if (ytFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                        chineseEntity.setImgytnm(fileName);
                        chineseEntity.setImgytpath(filePath);
                    }
                }
            }
            // 图片比较
            if(daFile != null && daFile.getOriginalFilename() !=null){
                if(!daFile.getOriginalFilename().startsWith("da", 0)){
                    // 获取文件名
                    String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_CHI);
                    if (daFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                        chineseEntity.setImgdanm(fileName2);
                        chineseEntity.setImgdapath(filePath2);
                    }                
                }
            }
            // 插入数据
            chineseEntity.setFxcs(1);
            chineseEntity.setXueke(chineseEntity.getHidXueke());

            Integer updRes=chineseService.updChinese(chineseEntity);

            if(updRes==1){
                model.addAttribute("msg", "修改成功！");
            }else{
                model.addAttribute("msg", "修改失败！");
            } 
            List<ChineseEntity> chineseByID = chineseService.getChineseByID(chineseEntity.getId());           
            if(chineseByID.isEmpty()){
               chineseByID = new ArrayList<ChineseEntity>();
            }
            model.addAttribute("chinese", chineseByID);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "文件上传发生错误,请重试!");
            
        }


        return "chi/modifyChinese";   
    }

    @PostMapping("/chineseDel")
    public String chineseDel(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {
        chineseService.delChinese(id);
        
        return "redirect:/chi/chineseInit";
    }    
    /************************************************Chinese End************************************************/    
}
